// 引入数据库模块
const mysql = require('mysql');
// 拉取配置项
var config = require('./config');

var connection = mysql.createConnection({
    host: config.host,
    user: config.db_user,
    password: config.db_password,
    port: config.db_port,
    database: config.db_name
});

/**
 * 运行SQL语句
 * @param { string } sql 数据库执行语句
 * @param { Function } sucCallback 成功的回调方法
 * @param { Function } errCallback 失败的回调方法
 */
function ExecuteSQL(sql, values, sucCallback, errCallback) {

    connection = mysql.createConnection(connection.config);

    connection.connect((err) => {
        if (err) {
            errCallback(err);
            return;
        }
        else
        {
            //console.log('[connection connect]  succeed!');
        }
    });

    if(values == null) {
        connection.query(sql, (error, results, fields)=> {
            if (error) {
                errCallback(error);
                //return;
            }
            else
            {
                sucCallback(results);
            }
        });
    }
    else
    {
        connection.query(sql, values, (error, results, fields)=> {
            if (error) {
                errCallback(error);
                //return;
            }
            else
            {
                sucCallback(results);
            }
        });
    }

    connection.end((err) => {
        if (err) {
            errCallback(err);
            return;
        }
        else
        {
            //console.log('[connection close]  succeed!');
        }
    });
}

let pool = mysql.createPool({
    host: config.host,
    user: config.db_user,
    password: config.db_password,
    port: config.db_port,
    database: config.db_name,
    acquireTimeout: 15000, // 连接超时时间
    connectionLimit: 100, // 最大连接数
    waitForConnections: true, // 超过最大连接时排队
    queueLimit: 0 // 排队最大数量(0 代表不做限制)
});

let query = function(sql, values) {
    return new Promise((resolve, reject) => {
        pool.getConnection(function (err, conn) {
            if(err) {
                reject(err);
            }
            else if( values == null )
            {
                conn.query(sql, (error, results, fields)=> {
                    // When done with the connection, release it.
                    conn.release();
                    //pool.releaseConnection(conn);
                    if (error) {
                        reject(error);
                    }
                    else {
                        resolve(results);
                    }
                });
            }
            else
            {
                conn.query(sql, values, (error, results, fields)=> {
                    // When done with the connection, release it.
                    conn.release();
                    //pool.releaseConnection(conn);
                    if (error) {
                        reject(error);
                    }
                    else {
                        resolve(results);
                    }
                });
            }
        });
    });
}

exports.ExecuteSQL = ExecuteSQL;
exports.query = query;
exports.config = config;